package com.dzm.phone.utils;

import lombok.Data;

/**
 * @author Dzm
 * @create 2024-04-11-15:01
 */
//统一结果类
@Data
public class Result<T> {

    private  Boolean success;
    private Integer code;
    private String message;
    private T data;

    private Result(){
    }

    private static <T> Result<T> builder(T data, ResultCode resultCode) {

        Result<T> result = new Result<>();
        if(data != null) {
            result.setData(data);
        }
        result.setMessage(resultCode.getMessage());
        result.setCode(resultCode.getCode());
        result.setSuccess(false);

        if(resultCode.getCode() == 200){
            result.setSuccess(true);
        }

        return result;
    }

    public static <T> Result<T> ok(){
        return builder(null, ResultCode.SUCCESS);
    }

    public static <T> Result<T> ok(T data){
        return builder(data, ResultCode.SUCCESS);
    }

    public static <T> Result<T> error(){
        return builder(null, ResultCode.FAIL);
    }

    public static <T> Result<T> error(T data){
        return builder(data, ResultCode.FAIL);
    }

    public static <T> Result<T> error(T data, ResultCode resultCode){
        return builder(data, resultCode);
    }

    public Result<T> message(String message){
        this.setMessage(message);
        return this;
    }

    public Result<T> success(Boolean success){
        this.setSuccess(success);
        return this;
    }

    public Result<T> code(Integer code) {
        this.setCode(code);
        return this;
    }

}
